package xyz.xiaoshengfu.xrpc.consumer;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import xyz.xiaoshengfu.xrpc.config.ProviderServiceInfor;
import xyz.xiaoshengfu.xrpc.data.XRpcResponseData;

/**
 * <p>Title: XRpcConsumerInvokeClientHandler</p>
 * <p>Description: xRPC消费者执行Handler</p>
 * @author xiaoshengfu(2439323118@qq.com)
 * @date 2020年2月25日
 */
public final class XRpcConsumerInvokeClientHandler extends SimpleChannelInboundHandler<XRpcResponseData> {

	private XRpcConsumerInvokeClient client;

	public XRpcConsumerInvokeClientHandler(XRpcConsumerInvokeClient client) {
		this.client = client;
	}

	/**
	* 数据处理
	*/
	@Override
	public void channelRead0(ChannelHandlerContext context, XRpcResponseData data) throws Exception {
		//System.out.println("XRpcConsumerInvokeClientHandler" + data);
		if (client.isAuth()) {
			client.setReturnValueAndNotify(data);
		} else {
			if (data.getStatusCode() != null) {
				if (data.getStatusCode() == XRpcResponseData.AUTH_SUCCESS) {
					client.setAuth(true);
					client.sendResultQueue();
				} else if (data.getStatusCode() == XRpcResponseData.AUTH_FAIL) {
					client.getProviderServiceInfor().setServerState(ProviderServiceInfor.ERROR);
					XRpcConsumerInvokeClient.removeErrorClient(client.getProviderServiceInfor().getApplicationName());
					XRpcConsumerSubscribeClient
							.removeErrorApplicationServer(client.getProviderServiceInfor().getApplicationName());
					Channel channel = context.channel();
					if (channel.isActive()) {
						context.close();
					}
				}
			}
		}
	}

	/**
	 * 通道建立成功操作
	 */
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		client.getProviderServiceInfor().setServerState(ProviderServiceInfor.OK);
	}

	/**
	 * 通道断开
	 */
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		if (client.getProviderServiceInfor().getServerState() != ProviderServiceInfor.ERROR) {
			client.getProviderServiceInfor().setServerState(ProviderServiceInfor.DISCONNECT);
			client.setAuth(false);
			client.doConnect();
		}
	}

	/**
	 * 异常处理
	 */
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		Channel channel = ctx.channel();
		if (channel.isActive()) {
			ctx.close();
		}
	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		if (evt instanceof IdleStateEvent) {
			IdleStateEvent e = (IdleStateEvent) evt;
			switch (e.state()) {
			case READER_IDLE:
				handleReaderIdle(ctx);
				break;
			case WRITER_IDLE:
				handleWriterIdle(ctx);
				break;
			case ALL_IDLE:
				handleAllIdle(ctx);
				break;
			default:
				break;
			}
		}
	}

	/**
	 *<p>Title: handleReaderIdle</p>
	 *<p>Description: Channel在规定时间内没有读操作触发时间的回调函数</p>
	 * @param ctx
	 */
	private void handleReaderIdle(ChannelHandlerContext ctx) {
	}

	/**
	 *<p>Title: handleWriterIdle</p>
	 *<p>Description: Channel在规定时间内没有写操作触发时间的回调函数</p>
	 * @param ctx
	 */
	private void handleWriterIdle(ChannelHandlerContext ctx) {
	}

	/**
	 *<p>Title: handleAllIdle</p>
	 *<p>Description: Channel在规定时间内没有读和写操作触发时间的回调函数</p>
	 * @param ctx
	 */
	private void handleAllIdle(ChannelHandlerContext ctx) {
	}
}